package g2.routemaster.gui.contents;

import g2.routemaster.gui.GManager;
import g2.routemaster.gui.general.EBonus;
import g2.routemaster.gui.general.ECityCard;
import g2.routemaster.gui.general.ImageRegistry;
import g2.routemaster.model.BonusPile;
import g2.routemaster.model.BonusTile;
import g2.routemaster.model.City;
import g2.routemaster.model.GameManager;
import g2.routemaster.model.LengthTile;
import g2.routemaster.model.Player;
import g2.routemaster.model.Region;

import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

public class BoardGroup extends Composite {
   Image imageMap;

   CLabel map;

   public BoardGroup(Composite parent, int style) {
      super(parent, style);
      imageMap = new Image(Display.getCurrent(), getClass()
            .getResourceAsStream("/pic/map.png"));
   }

   void cleanMap() {
      imageMap = new Image(Display.getCurrent(), getClass()
            .getResourceAsStream("/pic/map.png"));
      map.setImage(imageMap);
      map.redraw();
   }

   public void create() {
      Composite group = new Composite(this, SWT.NONE);
      group.setLayout(new FillLayout());

      map = new CLabel(group, SWT.SHADOW_OUT);
      map.setImage(imageMap);

      map.addListener(SWT.MouseMove, new Listener() {
         public void handleEvent(Event e) {
            System.out.println("Move: (" + (e.x + 2) + ", " + (e.y - 10) + ")");
         }
      });

      map.addListener(SWT.MouseDown, new Listener() {
         public void handleEvent(Event e) {
            System.out.println("Down: (" + e.x + ", " + e.y + ")");
         }
      });
   }

   Font getBonusFont() {
      Font font = Display.getCurrent().getSystemFont();
      FontData fontData = font.getFontData()[0];
      fontData.setStyle(SWT.BOLD);
      fontData.setHeight(12);
      return new Font(Display.getCurrent(), fontData);
   }

   Font getLengthFont() {
      Font font = Display.getCurrent().getSystemFont();
      FontData fontData = font.getFontData()[0];
      fontData.setStyle(SWT.BOLD);
      fontData.setHeight(8);
      return new Font(Display.getCurrent(), fontData);
   }

   Image getImageMap() {
      return imageMap;
   }

   public void updateData() {
      cleanMap();
      GameManager gm = GManager.instance().getGameManager();
      Iterator iter = gm.getBoard().iteratorOfBonusPile();
      GC gc = new GC(getImageMap());
      gc.setFont(getBonusFont());

      while (iter.hasNext()) {
         BonusPile bonusPile = (BonusPile) iter.next();
         String name = bonusPile.getName();
         BonusTile tile;
         int value;
         tile = bonusPile.getLastOfBonusTiles();
         if (tile != null) {
            value = (tile != null) ? tile.getValue() : 0;
            EBonus bonus = EBonus.byNom(name);
            switch (bonus) {
               case BADEN:
                  drawRegionBonus(gc, new RGB(183, 138, 240), new RGB(183, 138,
                        240), new Point(bonus.x(), bonus.y()), value);
                  break;
               case BAIERN:
                  drawRegionBonus(gc, new RGB(126, 184, 167), new RGB(126, 184,
                        167), new Point(bonus.x(), bonus.y()), value);
                  break;
               case BOHMEN_SALZBURG:
                  drawRegionBonus(gc, new RGB(201, 135, 142), new RGB(232, 109,
                        166), new Point(bonus.x(), bonus.y()), value);
                  break;
               case SCHWEIZ_TYROL:
                  drawRegionBonus(gc, new RGB(84, 138, 239), new RGB(246, 72,
                        20), new Point(bonus.x(), bonus.y()), value);
                  break;
               case WYRTTEMBERG_HOHENZOLLERN:
                  drawRegionBonus(gc, new RGB(128, 248, 242), new RGB(62, 248,
                        135), new Point(bonus.x(), bonus.y()), value);
                  break;
               case LENGTH5:
               case LENGTH6:
               case LENGTH7:
                  drawLengthBonus(gc, new Point(bonus.x(), bonus.y()),
                        ((LengthTile) tile).getLength(), value);
                  break;
            }
         }
      }

      Iterator iterRegion = gm.getBoard().iteratorOfRegion();
      while (iterRegion.hasNext()) {
         Region region = (Region) iterRegion.next();
         Iterator iterCity = region.iteratorOfCity();
         while (iterCity.hasNext()) {
            City city = (City) iterCity.next();
            Player aPlayer = gm.getFirstPlayer();
            while (aPlayer != null) {
               System.out.println("Player" + aPlayer.getName()
                     + " has house in " + city.getName() + " "
                     + aPlayer.hasABranchIn(city) + " seat: "
                     + aPlayer.getSeatPlace().getPlaceNr());

               if (aPlayer.hasABranchIn(city)) {
                  createFlag(gc, ECityCard.byNom(city.getName()), ImageRegistry
                        .getPlayerRGB(aPlayer.getColor().getName()), aPlayer
                        .getSeatPlace().getPlaceNr());
                  System.out.println(city.getName());
                  System.out.println(aPlayer.getColor().getName());
                  System.out.println(aPlayer.getSeatPlace().getPlaceNr());
               }
               aPlayer = gm.getNextPlayer(aPlayer);
               if (aPlayer != null && aPlayer.equals(gm.getFirstPlayer())) {
                  break;
               }
            }
         }
      }

      gc.dispose();
      map.redraw();
   }

   void drawRegionBonus(GC gc, RGB color1, RGB color2, Point place, int value) {
      // draw bonus
      gc.setForeground(new Color(Display.getCurrent(), color1));
      gc.setBackground(new Color(Display.getCurrent(), color2));
      gc.fillGradientRectangle(place.x - 8, place.y - 3, 25, 25, false);

      // draw line around rectangle
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
      gc.drawRectangle(place.x - 8, place.y - 3, 25, 25);

      // draw value
      gc.drawString(String.valueOf(value), place.x, place.y, true);
   }

   void drawLengthBonus(GC gc, Point place, int length, int value) {
      // draw bonus
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.fillRectangle(place.x - 8, place.y - 3, 25, 25);

      // draw line around rectangle
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
      gc.drawRectangle(place.x - 8, place.y - 3, 25, 25);

      // draw value
      gc.setFont(getLengthFont());
      gc.drawString(String.valueOf(length), place.x + 1, place.y - 4, true);

      gc.drawLine(place.x - 5, place.y + 2, place.x, place.y + 2);
      gc.drawLine(place.x + 9, place.y + 2, place.x + 14, place.y + 2);

      gc.setFont(getBonusFont());
      gc.drawString(String.valueOf(value), place.x, place.y + 4, true);
   }

   void testFillAll(GC gc) {
      for (ECityCard card : ECityCard.values()) {
         createFlag(gc, card, new RGB(128, 128, 128), 1);
         createFlag(gc, card, new RGB(0, 128, 128), 2);
         createFlag(gc, card, new RGB(128, 128, 0), 3);
         createFlag(gc, card, new RGB(128, 0, 128), 4);
      }
   }

   void createFlag(GC gc, ECityCard card, RGB color, int place) {
      int indent = 3;
      int width = 6;
      int height = 4;
      Point point = card.getPoint(place);

      // draw flag
      gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
      gc.drawRectangle(point.x - indent, point.y - indent, width + 1,
            height + 1);
      gc.drawLine(point.x - indent, point.y - indent + height,
            point.x - indent, point.y - indent + height + 5);

      // fill flag
      gc.setForeground(new Color(Display.getCurrent(), color));
      gc.setBackground(new Color(Display.getCurrent(), color));
      gc.drawRectangle(point.x - indent, point.y - indent, width, height);
      gc.fillRectangle(point.x - indent, point.y - indent, width, height);
   }

}
